<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="路线名陈" prop="routeName">
                <el-input
                    v-model="queryParams.routeName"
                    placeholder="请输入路线名陈"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    v-hasPermi="['system:route:add']"
                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="success"
                    icon="el-icon-edit"
                    size="mini"
                    :disabled="single"
                    @click="handleUpdate"
                    v-hasPermi="['system:route:edit']"
                >修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="danger"
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['system:route:remove']"
                >删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="warning"
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['system:route:export']"
                >导出
                </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="routeList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="编号" align="center" prop="id"/>
            <el-table-column label="路线名陈" align="center" prop="routeName"/>
            <el-table-column label="路线图" align="center" prop="stations">
                <div slot-scope="scope">
                    <div v-if="scope.row.stations != null">
                        <el-timeline :reverse="false">
                            <el-timeline-item
                                v-for="(item, index) in scope.row.stations"
                                :key="index"
                                size="large"
                                type="primary"
                                icon="el-icon-location">
                                {{item.stationName}}
                            </el-timeline-item>
                        </el-timeline>
                    </div>
                    <div v-else></div>
                </div>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createDate">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createDate) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="创建人" align="center" prop="createUserName"/>
            <el-table-column label="更新时间" align="center" prop="updateDate">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.updateDate) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="更新人" align="center" prop="updateUserName"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:route:edit']"
                    >修改
                    </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['system:route:remove']"
                    >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 新增弹出框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <route-add
                v-if="open"
                @onClose="routeClose"
                @on-confirm="addConfirm">
            </route-add>
        </el-dialog>

        <!--修改弹出框-->
        <el-dialog title="修改" :visible.sync="openUpdate" width="500px" append-to-body>
            <route-update
                v-if="openUpdate"
                :form-data="routeData"
                @onClose="updateClose"
                @on-confirm="updateConfirm"
            >
            </route-update>
        </el-dialog>
    </div>
</template>

<script>
    import {listRoute, getRoute, delRoute, addRoute, updateRoute, exportRoute} from "@/api/backstage/route";
    import RouteAdd from "./routeAdd";
    import RouteUpdate from "./routeUpdate";

    export default {
        name: "Route",
        components: {RouteUpdate, RouteAdd},
        data() {
            return {
                routeData:{},
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 【请填写功能名称】表格数据
                routeList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                openUpdate: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    routeName: null,
                },
                // 表单校验
                rules: {
                    routeName: [
                        {required: true, message: "路线名陈不能为空", trigger: "blur"}
                    ],
                    createDate: [
                        {required: true, message: "创建时间不能为空", trigger: "blur"}
                    ],
                    createUser: [
                        {required: true, message: "创建人不能为空", trigger: "blur"}
                    ],
                }
            };
        },
        mounted() {
            this.getList();
        },
        methods: {
            addConfirm(val){
                addRoute(val).then(resp => {
                    if (resp.msg === '新增成功') {
                        this.msgSuccess(resp.msg);
                        this.getList();
                    } else {
                        this.msgError(resp.msg);
                    }
                })
                this.open = false;
            },
            updateConfirm(val){
                updateRoute(val).then(resp => {
                    if (resp.msg === '更新成功') {
                        this.msgSuccess(resp.msg);
                        this.getList();
                    } else {
                        this.msgError(resp.msg);
                    }
                })
                this.openUpdate = false;
            },
            updateClose(){
                this.openUpdate = false;
                this.getList();
            },
            routeClose() {
                this.open = false;
                this.getList();
            },
            /** 查询【请填写功能名称】列表 */
            getList() {
                this.loading = true;
                listRoute(this.queryParams).then(response => {
                    this.routeList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.open = true;
                this.title = "添加";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.openUpdate = true;
                this.routeData = JSON.stringify(row);
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return delRoute(ids);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
            },
            /** 导出按钮操作 */
            handleExport() {
                const queryParams = this.queryParams;
                this.$confirm('是否确认导出所有【请填写功能名称】数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return exportRoute(queryParams);
                }).then(response => {
                    this.download(response.msg);
                })
            }
        }
    };
</script>
<style scoped>
</style>
